<?php
namespace  Admin\Controller;
use Think\Controller;

/**
 *
 * @author dema
 * @date  2015-3-30
 */
class SystemController extends AdminController{
    public function adminList(){
		$this->assign('_list',M('user')->select());
		$this->display();
	}

	public function addAdmin(){

		if(IS_POST){
			$password=I('post.password');
			$repassword=I('post.repassword');
			if($password != $repassword){
				$this->error('密码和重复密码不一致！');
			}
			$data=array(
					'name'=>I('post.name'),
					'status'=>I('post.status'),
					'remark'=>I('post.remark'),
			);
			empty($data) and $this->success('添加数据为空，添加失败');
			$uid=M('user')->add($data);
			if($uid){
				$role=I('post.role');
				M('role_user')->add(array('role_id'=>$role,'user_id'=>$uid));
				$this->success('添加管理员成功');
			}else{
				$this->error('添加失败');
			}
		}else{
			$rolelist=M('role')->where(array('status'=>1))->select();
			$this->assign('rolelist',$rolelist);
			$this->display();
		}
	}


	public function addNode(){
		if(IS_POST){
			$data=array(
					'name'=>I('post.nodename'), //节点
					'title'=>I('post.title'),   //
					'status'=>I('post.status','1','intval'),
					'remark'=>I('post.remark'),
					'sort'=>I('post.sort'),
					'pid'=>I('post.pid'),
					'level'=>I('post.level'),
					'ismenu'=>I('post.ismenu','1','intval'),
					'group_id'=>I('post.groupid'),
					'path'=>I('post.path'),
			);
			empty($data) and $this->error('添加数据为空，添加失败');
			M('node')->add($data);
			$this->success('添加成功');
		}else{
			$this->display();
		}
	}

	public function addRole(){

		if(IS_POST){
			$data=array(
					'name'=>I('post.rolename'),
					'status'=>I('post.status'),
					'remark'=>I('post.remark'),
					);
			empty($data) and $this->error('添加数据为空，添加失败');
			M('role')->add($data);
			$this->success('添加成功');
		}else{
			$this->display();
		}
	}

	public function roleList(){
		$this->assign('_list',M('role')->select());
		$this->display();
	}

	public function nodeList(){
		$this->assign('_list',M('node')->select());
		$this->display();
	}

	public function menuList(){
		$list=M('menu_groups')->field('id,name,status')->select();
		$this->assign('_list',$list);
		$this->display();
	}

	//
	public function addMenu(){
		if(IS_POST){
			$data=array(
					'name'=>I('post.menuname'),
					'status'=>I('post.status','0','intval'),
					);
			$add=M('menu_groups')->add($data);
			if($add){$this->success('添加菜单成功',U('System/menuList'));}
			$this->error('添加失败') ;
		}else{
			$this->display();
		}
	}

	public function giveRights(){
		$id=I('get.id');

		$data=M('node')->select();
		$node_list=array();
		foreach($data as $v){
			if($v['level'] == 2){
				$node_list[$v['id']]['id']=$v['id'];
				$node_list[$v['id']]['title']=$v['title'];
			}elseif($v['level'] == 3){
				$node_list[$v['pid']]['operator'][$v['id']]['id']=$v['id'];
				$node_list[$v['pid']]['operator'][$v['id']]['title']=$v['title'];
			}
		}
		$node_list2[]=array(
					'id'=>1,
					'title'=>'后台管理',
					'child'=>$node_list,
				);
		//dump($node_list2);
		$this->assign('rolelist',M('role')->select());
		$this->assign('id',$id);
		$this->assign('node_list',$node_list2);
		$this->display();
	}
}